package com.cxyzy.viewpager.showneighbour

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import kotlin.math.floor

class FlipFragment : Fragment() {
    private var card: ViewGroup? = null
    private var mContent = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        retainInstance = true
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val rootView = inflater.inflate(
            R.layout.fragment_flip,
            container, false
        ) as ViewGroup
        val bg = randomBgColor
        card = rootView.findViewById(R.id.card)
        card?.setBackgroundColor(bg)
        val title = rootView.findViewById<TextView>(R.id.title)
        title.text = "" + mContent
        title.setTextColor(Color.WHITE)
        return rootView
    }

    private val randomBgColor: Int
        private get() = Color.rgb(
            floor(Math.random() * 128).toInt() + 64,
            floor(Math.random() * 128).toInt() + 64,
            floor(Math.random() * 128).toInt() + 64
        )

    companion object {
        fun newInstance(content: String): FlipFragment {
            val fragment = FlipFragment()
            fragment.mContent = content
            return fragment
        }
    }
}